using UnityEngine;
using System.Collections;

public class ParticleManager : MonoBehaviour {

    public Transform particle;
    GameObject go;
    public bool canDispose =  false;
    void Start()
    {
        GameObject go = Resources.Load("Particle") as GameObject;
        particle = go.transform;

        StartCoroutine(instance());
    }

    IEnumerator instance()
    {
        yield return new WaitForSeconds(0.05f);
        if(renderer.material.color != Color.black && !canDispose && rigidbody.isKinematic == false)
        newObject();
        StartCoroutine(instance());
    }

    void newObject()
    {
        float y = 0;
        y = Random.Range(-0.5f, 0.5f);

        Transform go = Instantiate(particle, new Vector3(transform.position.x+y, transform.position.y+y, -0.99f), particle.rotation) as Transform;
        go.renderer.material.color = new Color(renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, 0);
    }

    void changeReady()
    {
        canDispose = !canDispose;
    }
}
